package 数组;

import javax.sound.midi.Soundbank;
import java.util.Arrays;

public class 稀疏数组 {
    public static void main(String[] args) {
        int[][] qp = new int[11][11];
        qp[1][2] = 1;
        qp[2][3] = 2;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                System.out.print(qp[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println("=========");
        System.out.println("创建稀疏数组");
        int ls = 1;
        for (int i = 0; i < qp.length; i++) {
            for (int j = 0; j < qp[i].length; j++) {
                if (qp[i][j] != 0) {
                    ls++;
                }
            }
        }
        int[][] xs = new int[ls][3];
        System.out.println("稀疏数组");
        //System.out.println(Arrays.(xs));
        for (int[] x : xs) {
            for (int i : x) {
                System.out.print(i + "\t");
            }
            System.out.println();
        }
        System.out.println("========");
        System.out.println("写入数据");

        xs[0][0] = qp.length;
        xs[0][1] = qp[0].length;
        xs[0][2] = ls-1;

        int xb = 1;
        for (int i = 0; i < qp.length; i++) {
            for (int j = 0; j < qp[i].length; j++) {
                if (qp[i][j] != 0) {
                    xs[xb][0] = i;
                    xs[xb][1] = j;
                    xs[xb][2] = qp[i][j];
                    xb++;
                }
            }
        }
        for (int[] x : xs) {
            for (int i : x) {
                System.out.print(i + "\t");
            }
            System.out.println();
        }
        System.out.println("========");
        System.out.println("还原稀疏数组");
        int[][] qp1=new int[xs[0][0]][xs[0][1]];
        for (int i = 1; i <xs.length ; i++) {
            qp1[xs[i][0]][xs[i][1]]=xs[i][2];
        }
        /**
         * 输出数组
         */
        for (int[] ints : qp1) {
            for (int anInt : ints) {
                System.out.print(anInt+"\t");
            }
            System.out.println();
        }
    }
}
